package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class ItemContainer here.
 * 
 * @author (dmab0913 Gruppe4) 
 * @version (a version number or a date)
 */
public class ItemContainer
{
    private ArrayList<Item> Items;
    private static ItemContainer instance;

    /**
     * Constructor for objects of class ItemContainer
     */
    public ItemContainer()
    {
        Items = new ArrayList<Item>();
    }

    /**
     * Singleton for the ItemContainer class
     */
    public static ItemContainer getInstance()
    {
        if (instance == null)
        {
            instance = new ItemContainer();
            return instance;
        }
        return instance;
    }

    /**
     * puts Item-objekt in the container
     */
    public void addItem(Item item)
    {
        Items.add(item);
    }

    /**
     * find a specific Item-objekt in the container
     */
    public Item findItem(int itemId)
    {
        int index = 0;
        boolean found = false;
        while(index < Items.size() && !found)
        {
            Item item = Items.get(index);
            if(itemId == item.getItemId())
            {
                found = true;
            }
            else
            {
                index++;
            }
        }

        if(found)
        {
            return Items.get(index);
        }
        else
        {
            return null;
        }
    }

    /**
     * removes a Item-objekt from the container
     */
    public void deleteItem(int itemId)
    {
        boolean found = false;
        int index = 0;
        while(index < Items.size() && !found)
        {
            Item l = Items.get(index);
            if(l.getItemId()==(itemId))
            {
                Items.remove(index);
                found = true;
            }
            index++;
        }
    }

    /**
     * finds and updates all the fields of a Item-objekt except for itemId
     */
    public void updateItem(int itemId, String newDescription,int newQuantity, double newPricePrPackage, int newSizePrPackage, double newPricePrItem, int newLocationNumber, int newMaxStock, int newMinStock)
    {
        boolean found = false;
        int index = 0;
        while(index < Items.size() && !found)
        {
            Item I = Items.get(index);
            if(I.getItemId()==(itemId))
            {
                I.updateItem(newDescription, newQuantity, newPricePrPackage, newSizePrPackage, newPricePrItem, newLocationNumber, newMaxStock, newMinStock);
                found = true;
            }
            index++;
        }
    }
}
